package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.model.AddressDO;
import net.xdclass.mapper.AddressMapper;
import net.xdclass.model.LoginUser;
import net.xdclass.model.UserDO;
import net.xdclass.request.AdressAddRequest;
import net.xdclass.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.vo.AddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电商-公司收发货地址表 服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2023-04-26
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public AddressVo detail(long id){
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id).eq("user_id",loginUser.getId()));
        if (ObjectUtils.isEmpty(addressDO)) {
            return null;
        }
        AddressVo addressVo = new AddressVo();
        BeanUtils.copyProperties(addressDO,addressVo);
        return addressVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(AdressAddRequest adressAddRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());
        BeanUtils.copyProperties(adressAddRequest,addressDO);
        if (addressDO.getDefaultStatus() == 1) {
            //如果已经有默认收货地址，就要更改收货地址
            AddressDO existDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("default_status", 1));
            if (!ObjectUtils.isEmpty(existDO)) {
                existDO.setDefaultStatus(0);
                addressMapper.updateById(existDO);
            }
        }

        int rows = addressMapper.insert(addressDO);
        log.info("新增收货地址:rows={},data={}",rows,addressDO);
        return rows;
    }

    @Override
    public int del(long address_id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        int rows = addressMapper.delete(new QueryWrapper<AddressDO>().eq("id",address_id).eq("user_id",loginUser.getId()));
        return rows;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<AddressVo> list() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
        List<AddressVo> addressVoList = addressDOList.stream().map(obj -> {
            AddressVo addressVo = new AddressVo();
            BeanUtils.copyProperties(obj, addressVo);
            return addressVo;
        }).collect(Collectors.toList());
        return addressVoList;
    }
}
